"""
GitCode 分支API
"""
from typing import Any, Dict, List, Optional

from ..base.client import BaseAPI


class BranchAPI(BaseAPI):
    """分支API"""
    
    async def list_branches(self, owner: str, repo: str, protected: Optional[bool] = None) -> List[Dict[str, Any]]:
        """
        获取仓库的分支列表
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            protected: 是否只返回受保护分支
            
        Returns:
            分支列表
        """
        params = {"protected": protected} if protected is not None else None
        return await self.request("GET", f"/repos/{owner}/{repo}/branches", params=params)
    
    async def get_branch(self, owner: str, repo: str, branch: str) -> Dict[str, Any]:
        """
        获取特定分支的详细信息
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            branch: 分支名称
            
        Returns:
            分支信息
        """
        return await self.request("GET", f"/repos/{owner}/{repo}/branches/{branch}")
    
    async def create_branch(self, owner: str, repo: str, branch: str, ref: str) -> Dict[str, Any]:
        """
        创建新分支
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            branch: 新分支名称
            ref: 源分支或提交SHA
            
        Returns:
            分支信息
        """
        data = {
            "branch": branch,
            "ref": ref
        }
        return await self.request("POST", f"/repos/{owner}/{repo}/branches", data=data)
    
    async def delete_branch(self, owner: str, repo: str, branch: str) -> None:
        """
        删除分支
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            branch: 分支名称
        """
        await self.request("DELETE", f"/repos/{owner}/{repo}/branches/{branch}")
    
    async def merge_branch(
        self,
        owner: str,
        repo: str,
        base: str,
        head: str,
        commit_message: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        合并分支
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            base: 目标分支名称
            head: 源分支名称
            commit_message: 合并提交消息
            
        Returns:
            合并结果
        """
        data = {
            "base": base,
            "head": head,
            "commit_message": commit_message
        }
        data = {k: v for k, v in data.items() if v is not None}
        
        return await self.request("POST", f"/repos/{owner}/{repo}/merges", data=data)
    
    async def protect_branch(
        self,
        owner: str,
        repo: str,
        branch: str,
        required_status_checks: Optional[Dict[str, Any]] = None,
        enforce_admins: Optional[bool] = None,
        required_pull_request_reviews: Optional[Dict[str, Any]] = None,
        restrictions: Optional[Dict[str, Any]] = None
    ) -> Dict[str, Any]:
        """
        保护分支
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            branch: 分支名称
            required_status_checks: 状态检查设置
            enforce_admins: 是否对管理员强制执行
            required_pull_request_reviews: PR审查设置
            restrictions: 限制设置
            
        Returns:
            保护规则
        """
        data = {
            "required_status_checks": required_status_checks,
            "enforce_admins": enforce_admins,
            "required_pull_request_reviews": required_pull_request_reviews,
            "restrictions": restrictions
        }
        data = {k: v for k, v in data.items() if v is not None}
        
        return await self.request("PUT", f"/repos/{owner}/{repo}/branches/{branch}/protection", data=data)
    
    async def remove_branch_protection(self, owner: str, repo: str, branch: str) -> None:
        """
        移除分支保护
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            branch: 分支名称
        """
        await self.request("DELETE", f"/repos/{owner}/{repo}/branches/{branch}/protection")
    
    async def get_commit(self, owner: str, repo: str, ref: str) -> Dict[str, Any]:
        """
        获取提交信息
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            ref: 提交SHA或引用名称
            
        Returns:
            提交信息
        """
        return await self.request("GET", f"/repos/{owner}/{repo}/commits/{ref}")
    
    async def list_commits(
        self,
        owner: str,
        repo: str,
        sha: Optional[str] = None,
        path: Optional[str] = None,
        author: Optional[str] = None,
        since: Optional[str] = None,
        until: Optional[str] = None,
        per_page: Optional[int] = None,
        page: Optional[int] = None
    ) -> List[Dict[str, Any]]:
        """
        列出提交
        
        Args:
            owner: 仓库所有者
            repo: 仓库名称
            sha: 分支/提交SHA/标签名称
            path: 文件路径过滤
            author: 作者GitHub登录名
            since: 开始日期（ISO 8601格式）
            until: 结束日期（ISO 8601格式）
            per_page: 每页结果数
            page: 页码
            
        Returns:
            提交列表
        """
        params = {
            "sha": sha,
            "path": path,
            "author": author,
            "since": since,
            "until": until,
            "per_page": per_page,
            "page": page
        }
        params = {k: v for k, v in params.items() if v is not None}
        
        return await self.request("GET", f"/repos/{owner}/{repo}/commits", params=params) 